﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TXTD.Admin.Data.Entitys.System;
using TXTD.Admin.Interface.System;
using TXTD.Admin.Model.Modules.System;
using TXTD.Admin.Model.Paging;

namespace TXTD.Admin.Service.System
{
    public class SysMenuService : BaseService<SysMenu>, ISysMenuService
    {
        public SysMenuService(DbContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
        {
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public List<SysMenuModel> GetMenuList()
        {
            List<SysMenu> list = QueryAllData().OrderBy(t => t.MenuSort).ToList();

            List<SysMenuModel> result = GetChilden(list, null);

            return result;
        }
        /// <summary>
        /// 获取子级菜单
        /// </summary>
        /// <param name="list"></param>
        /// <param name="menuId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        private List<SysMenuModel> GetChilden(List<SysMenu> list, string? menuId)
        {
            List<SysMenuModel> children = new List<SysMenuModel>();
            foreach (SysMenu menu in list.Where(t => t.ParentMenuId == menuId))
            {
                children.Add(new SysMenuModel()
                {
                    MenuId = menu.MenuId,
                    ParentMenuId = menu.ParentMenuId,
                    MenuTitle = menu.MenuTitle,
                    MenuName = menu.MenuName,
                    MenuPath = menu.MenuPath,
                    MenuType = menu.MenuType,
                    MenuSort = menu.MenuSort,
                    ComponentPath = menu.ComponentPath,
                    OpenWay = menu.OpenWay,
                    menuPermissions = menu.MenuPermissions,
                    Children = GetChilden(list, menu.MenuId)
                });
            }
            return children;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Add(SysMenu model)
        {
            return InsertModel(model);
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Update(SysMenu model)
        {
            return UpdateModel(model, model.MenuId);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public int DeleteById(string id)
        {
            return RemoveById(id);
        }
        /// <summary>
        /// 根据登录用户获取菜单
        /// </summary>
        /// <returns></returns>
        public List<SysMenuModel> GetLoginUserMenuList()
        {
            List<SysMenu> list = new List<SysMenu>();
            if (LoginUser != null)
            {
                if (LoginUser.UserCode == "admin")
                {
                    list = db.Set<SysMenu>().OrderBy(t => t.MenuSort).ToList();
                }
                else
                {
                    list = (from a in db.Set<SysMenu>()
                            join b in db.Set<SysRoleMenu>() on a.MenuId equals b.MenuId
                            join c in db.Set<SysUserRole>().Where(t => t.UserId == LoginUser.UserId) on b.RoleId equals c.RoleId
                            select a).Distinct().OrderBy(t => t.MenuSort).ToList();

                    //处理父级菜单
                    List<SysMenu> parentList = new List<SysMenu>();
                    foreach (var item in list)
                    {
                        var lastData = list.FirstOrDefault(t => t.MenuId == item.ParentMenuId);
                        if (lastData == null)
                        {
                            parentList.AddRange(GetParent(item));
                        }
                    }
                    list.AddRange(parentList);
                    //去重
                    list = list.Distinct().ToList();
                }
            }

            return GetChilden(list, null); ;
        }
        /// <summary>
        /// 获取所有父级菜单
        /// </summary>
        /// <param name="list"></param>
        /// <param name="item"></param>
        private List<SysMenu> GetParent(SysMenu item)
        {
            List<SysMenu> result = new List<SysMenu>();
            if (item != null && item.ParentMenuId != null)
            {
                SysMenu parent = db.Set<SysMenu>().Find(item.ParentMenuId);
                result.Add(parent);

                result.AddRange(GetParent(parent));
            }
            return result;
        }
    }
}
